#!/bin/bash

# build an icon theme from svg source - a bit hard coded :P
# (c) Michael Amadio, 2016 - GPLv2

[ -f ./build.conf ] && . ./build.conf
[ -z "$NAME" ] &&  NAME=Generic
[ -z "$COMMENT" ] && COMMENT="An Icon Theme"
THEME="$NAME"
echo "Please wait. Processing $THEME ..."
[ -d "$THEME" ] && rm -r "$THEME"
TMP0=$(mktemp -p /tmp icn.XXXXXXXXX)
TMP1=$(mktemp -p /tmp fix.XXXXXXXXX)
(
cd icons
# find each icon
while read -r line;do
	[ "${line:0:1}" = "#" ] && continue
	[ -z "$line" ] && continue
	# process, construct each icon
	while read -r a b c; do 
		[ -d "../${THEME}/${a}" ] || mkdir -p "../${THEME}/${a}"
		SIZE=${a%/*}
		[ "$SIZE" = 'scalable' ] && SIZE=256 # fix scalable
		while read -r ent;do
			[ -z "$ent" ] && continue # skip blank lines
			if [ "${ent:0:4}" = '<svg' ];then
				if echo "$ent" | grep -q 'xlink';then
					printf "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.1\" height=\"${SIZE}px\" width=\"${SIZE}px\" viewBox=\"0 0 100 100\">\n" >> "../${THEME}/${a}/${b}"
				else
					printf "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" height=\"${SIZE}px\" width=\"${SIZE}px\" viewBox=\"0 0 100 100\">\n" >> "../${THEME}/${a}/${b}"
				fi
			elif [ "${ent:0:5}" = '<?xml' -o "${ent:0:6}" = '</svg>' ];then
				printf "%s\n" "${ent}" >> "../${THEME}/${a}/${b}"
			else
				printf "  %s\n" "${ent}" >> "../${THEME}/${a}/${b}"
			fi
		done <<< "$(cat $b)"
		# link(s) to icon if needed
		for link in `echo "$c"`;do
			[ -z "$b" ] && continue
			(cd "../${THEME}/${a}"; ln -sf "$b" "$link")
		done
	done <<< "$line"
done < ../theme.conf
)

# constuct the index.theme
echo '[Icon Theme]' > "${THEME}"/index.theme
echo "Name=${THEME}" >> "${THEME}"/index.theme
echo "Inherits=Hicolor" >> "${THEME}"/index.theme
echo "Comment=$COMMENT" >> "${THEME}"/index.theme
echo -n "Directories=" >> "${THEME}"/index.theme
(cd "${THEME}"; find ./16 ./22 ./24 ./32 ./48 ./64 ./scalable -type d -mindepth 1) > $TMP0
while read -r w;do
	echo -n "${w#*/}," >> $TMP1
done < $TMP0
cat "$TMP1" | sed 's/,$//' >> "${THEME}"/index.theme
rm $TMP1
echo "" >> "${THEME}"/index.theme 
while read -r y;do
	yy=${y#*/}
	py=${yy#*/}
	echo "" >> "${THEME}"/index.theme  
	case $yy in 
		scalable*)
			echo "[${yy}]" >> "${THEME}"/index.theme
			echo "Size=256" >> "${THEME}"/index.theme
			echo "Context=${py^}" >> "${THEME}"/index.theme #first char upper
			echo "Type=Scalable" >> "${THEME}"/index.theme
			echo "MinSize=8" >> "${THEME}"/index.theme
			echo "MaxSize=512" >> "${THEME}"/index.theme
			;;
		*)
			echo "[${yy}]" >> "${THEME}"/index.theme
			SIZE=${yy%/*}
			echo "Size=${SIZE}" >> "${THEME}"/index.theme
			echo "Context=${py^}" >> "${THEME}"/index.theme
			echo "Type=fixed" >> "${THEME}"/index.theme
			;;
	esac
done < $TMP0
rm $TMP0
# pack it up
pkg=`echo "${THEME}"|sed 's/ /_/'` # replace spaces
echo "Packaging ${THEME} as ${pkg}.tar.xz."
[ -f "${pkg}.tar.xz" ] && rm "${pkg}.tar.xz"
tar -cJf ${pkg}.tar.xz "${THEME}"
echo 'Done!'
